<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限列表</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <script src="/js/axios.min.js"></script>
    <script src="/js/vue.js"></script>
    <script src="/js/qs.min.js"></script>
    <script src="/js/jquery.js"></script>

    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        .cell{
            text-align: center;
        }
    </style>
</head>
<body>
<h3>权限列表</h3>
<div id="app">
    <template>
        <el-table style="width: 100%; text-align: center"
                  :data="functions"
                  ref="roleTable">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column label="id" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column label="权限" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column label="描述" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.description }}</span>
                </template>
            </el-table-column>
            <el-table-column label="url" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.url }}</span>
                </template>
            </el-table-column>
            <el-table-column label="pid" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.pid }}</span>
                </template>
            </el-table-column>
            <el-table-column label="icon" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.icon }}</span>
                </template>
            </el-table-column>
            <el-table-column label="sort" width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.sort }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-popconfirm icon-color="red" title="确定删除吗？" @confirm="handleDelete(scope.$index, scope.row)">
                        <el-button size="mini" icon="el-icon-delete" type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <div class="block" style="margin-top: 10px;">
            <el-pagination @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="currentPage"
                           :page-sizes="page_size"
                           :page-size="4"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="count"
                           :background="back">
            </el-pagination>
        </div>
    </template>

    <el-button style="margin-top: 10px;" icon="el-icon-plus" size="mini" @click="dialogFormVisible = true">添加权限</el-button>

    <el-dialog title="角色操作" :visible.sync="dialogFormVisible" width="500px" @close="closeDialog()">
        <el-form :model="fun">
            <el-form-item label="权限id">
                <el-input v-model="fun.id" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="权限名">
                <el-input v-model="fun.name" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="权限描述">
                <el-input v-model="fun.description" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="url">
                <el-input v-model="fun.url" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="pid">
                <el-input v-model="fun.pid" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="icon">
                <el-input v-model="fun.icon" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <el-form :model="fun">
            <el-form-item label="sort">
                <el-input v-model="fun.sort" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveFunction()">确 定</el-button>
        </div>
    </el-dialog>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                functions:[],
                // 当前页数
                currentPage: 1,
                // 订单总数
                count : 0,
                // 每页的数据
                page_size : [6,3],
                // 当前每页的数据
                pageSize : 6,
                // 分页背景
                back: true,
                fun: {},
                // 修改角色模态框
                dialogFormVisible: false,
                // 区分添加和更新角色
                flag: 0,
            }
        },
        methods: {
            // 加载所有权限
            loadFunctions() {
                axios.get("/functions/"+this.currentPage+"/"+this.pageSize)
                    .then(res => {
                        this.functions = res.data.data;
                        this.count = res.data.count;
                    })
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.loadFunctions();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.loadFunctions();
            },
            // 更新,添加权限
            saveFunction() {
                if(this.flag == 0){
                    axios.post("/functions", this.fun)
                        .then(res => {
                            if(res.data == "ok"){
                                this.loadFunctions();
                            }
                        });
                }else {
                    axios.put("/functions", this.fun)
                        .then(res => {
                            if(res.data == "ok"){
                                this.loadFunctions();
                            }
                        });
                }
                this.dialogFormVisible = false;
            },
            // 通过id查询角色
            handleEdit(index, row) {
                this.flag = 1;
                axios.get("/function/" + row.id)
                    .then(res => {
                        this.fun = res.data;
                    });
                this.dialogFormVisible = true;
            },
            // 删除角色
            handleDelete(index, row) {
                if(this.functions.length == 1){
                    this.currentPage = this.currentPage - 1;
                }
                axios.delete("/functions/" + row.id)
                    .then(res => {
                        if(res.data == "ok"){
                            this.loadFunctions();
                        }
                    });
            },
            //清空修改权限模态框数据
            closeDialog() {
                this.flag = 0;
                this.role = {};
            },
        },
        mounted() {
            this.loadFunctions();
        }
    });
</script>
</html>